Ext.define('App.activiti.editor.nbmp.view.NbmpList', {
    extend: 'Extend.grid.CrudGridPanel',
    alias: 'widget.NbmpList',
    requires: ['App.activiti.editor.nbmp.model.NbmpViewModel'],
    viewModel: 'NbmpViewModel',
    config: {
        tbar: {
            quickCreate: {
                hidden: true
            },
            publish: {
                index: 10,
                text: '部署',
                iconCls: 'publish',
                xtype: 'button',
                disabled: true,
                handler: function () {
                    var me = this.up('CrudGridPanel');
                    var records = me.getSelectedRecords();
                    me.publishHandler.call(me, records)
                }
            },
            revoke: {
                index: 10,
                text: '取消部署',
                iconCls: 'revoke',
                xtype: 'button',
                disabled: true,
                handler: function () {
                    var me = this.up('CrudGridPanel');
                    var records = me.getSelectedRecords();
                    me.revokeHandler.call(me, records)
                }
            }
        }
    },
    bind: {
        store: '{Query}',
        columns: '{columns}',
        search: '{search}'
    },
    editor: {
        formClazz: 'App.activiti.editor.nbmp.view.NbmpEditor',
        save: 'activiti/model/nbmp/save',
        get: 'activiti/model/nbmp/get_by_id',
        del: 'activiti/model/nbmp/delete'
    },
    resetButtons: function (selected) {
        this.callParent(arguments);
        var revoke = this
            .down('button[action="revoke"]');
        var publish = this
            .down('button[action="publish"]');
        if (selected.length == 1 && selected[0].get('publish') == true) {
            revoke.enable();
        } else {
            revoke.disable();
        }
        if (selected.length == 1) {
            publish.enable();
        } else {
            publish.disable();
        }
    },
    publishHandler: function (records) {
        var me = this;
        me.mask('部署...')
        if (records.length == 1) {
            Ext.Ajax.request({
                url: 'activiti/model/nbmp/publish',
                params: {id: records[0].get('id')},
                success: function (resp) {
                    var result = resp.result;
                    try {
                        if (result.success) {
                            me.deselectAll();
                            me.reload();
                        }
                    } finally {
                        me.unmask();
                    }
                },
                failure: function () {
                    me.unmask();
                }
            });
        } else if (records.length > 1) {
            Ext.Msg.alert("错误", "选多了")
        }
    },
    revokeHandler: function (records) {
        var me = this;
        me.mask('撤销部署...')
        if (records.length == 1) {
            Ext.Ajax.request({
                url: 'activiti/model/nbmp/revoke',
                params: {id: records[0].get('id')},
                success: function (resp) {
                    var result = resp.result;
                    try {
                        if (result.success) {
                            me.deselectAll();
                            me.reload();
                        }
                    } finally {
                        me.unmask();
                    }
                },
                failure: function () {
                    me.unmask();
                }
            });
        } else {
            Ext.Msg.alert("错误", "选多了或少了")
        }
    }
});